package com.zankong.tool.zkappforas.div_item;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.eclipsesource.v8.V8Function;
import com.google.zxing.EncodeHintType;
import com.zankong.tool.zkapp.document.ZKDocument;
import com.zankong.tool.zkapp.item.HolderInit;
import com.zankong.tool.zkapp.item.ZKAdapter;
import com.zankong.tool.zkapp.util.Util;
import com.zankong.tool.zkapp.util.ZKAppAdapter;
import com.zankong.tool.zkappforas.R;

import org.dom4j.Attribute;
import org.dom4j.Element;

import java.util.HashMap;

/**
 * Created by YF on 2018/8/31.
 */

@ZKAppAdapter("task_agree")
public class Task_Agree extends ZKAdapter {

    private final int STATUS_ROBBING = 0;
    private final int STATUS_RUNNING = 1;
    private final int STATUS_EVALUATE = 2;
    private final int STATUS_FINISH = 3;
    private HashMap<String,Integer> mStatus = new HashMap<String,Integer>(){{
        put("robbing",STATUS_ROBBING);
        put("running",STATUS_RUNNING);
        put("evaluate",STATUS_EVALUATE);
        put("finish",STATUS_FINISH);
    }};
    private HashMap<String,String> mClickMap = new HashMap<>();

    public Task_Agree(ZKDocument zKDocument, Element element) {
        super(zKDocument, element);
        for (Attribute attribute : element.element("task_agree").attributes()) {
            mClickMap.put(attribute.getName(),attribute.getValue());
        }
    }

    @Override
    protected RecyclerView.ViewHolder onCreate(@NonNull ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (i){
            case STATUS_ROBBING:
                viewHolder = new RobbingHolder(mLayoutInflater.inflate(R.layout.item_task_agree_robbing,viewGroup,false));
                break;
            case STATUS_RUNNING:
                viewHolder = new RunningHolder(mLayoutInflater.inflate(R.layout.item_task_agree_running,viewGroup,false));
                break;
            case STATUS_EVALUATE:
                viewHolder = new EvaluateHolder(mLayoutInflater.inflate(R.layout.item_task_agree_evaluate,viewGroup,false));
                break;
            case STATUS_FINISH:
                viewHolder = new FinishHolder(mLayoutInflater.inflate(R.layout.item_task_agree_finish,viewGroup,false));
                break;
        }
        return viewHolder;
    }

    @Override
    protected int getVIewType(int position) {
        return mStatus.get(mList.get(position).getString("status"));
    }

    @Override
    protected void onBind(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof HolderInit){
            ((HolderInit) viewHolder).init(position);
        }
    }

    private class RobbingHolder extends RecyclerView.ViewHolder implements HolderInit{
        Button chat,cancel;
        public RobbingHolder(@NonNull View itemView) {
            super(itemView);
            chat = itemView.findViewById(R.id.chat);
            cancel = itemView.findViewById(R.id.cancel);
        }

        @Override
        public void init(int position) {
            chat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mClickMap.containsKey("chat")){
                        invokeWithContext(mClickMap.get("chat"),RobbingHolder.this);
                    }
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mClickMap.containsKey("cancel")){
                        invokeWithContext(mClickMap.get("cancel"),RobbingHolder.this);
                    }
                }
            });
        }
    }
    private class RunningHolder extends RecyclerView.ViewHolder implements HolderInit{
        Button chat,cancel,complete;
        public RunningHolder(@NonNull View itemView) {
            super(itemView);
            chat = itemView.findViewById(R.id.chat);
            cancel = itemView.findViewById(R.id.cancel);
            complete = itemView.findViewById(R.id.complete);
        }

        @Override
        public void init(int position) {
            chat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mClickMap.containsKey("chat")){
                        invokeWithContext(mClickMap.get("chat"),RunningHolder.this);
                    }
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mClickMap.containsKey("cancel")){
                        invokeWithContext(mClickMap.get("cancel"),RunningHolder.this);
                    }
                }
            });
            complete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mClickMap.containsKey("complete")){
                        invokeWithContext(mClickMap.get("complete"),RunningHolder.this);
                    }
                }
            });
        }
    }
    private class EvaluateHolder extends RecyclerView.ViewHolder implements HolderInit{
        Button chat,evaluate;

        public EvaluateHolder(@NonNull View itemView) {
            super(itemView);
            chat = itemView.findViewById(R.id.chat);
            evaluate = itemView.findViewById(R.id.evaluate);
        }

        @Override
        public void init(int position) {
            chat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mClickMap.containsKey("chat")){
                        invokeWithContext(mClickMap.get("chat"),EvaluateHolder.this);
                    }
                }
            });
            evaluate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mClickMap.containsKey("evaluate")){
                        invokeWithContext(mClickMap.get("evaluate"),EvaluateHolder.this);
                    }
                }
            });
        }
    }
    private class FinishHolder extends RecyclerView.ViewHolder implements HolderInit{
        public FinishHolder(@NonNull View itemView) {
            super(itemView);
        }

        @Override
        public void init(int position) {

        }
    }



}
